package com.hantang.web.base;

import java.io.IOException;
import java.lang.reflect.Field;

import org.apache.log4j.Logger;
import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.NetworkListener;
import org.glassfish.grizzly.threadpool.ThreadPoolConfig;

//import com.smartclouder.dfsdatabase.StorageUtility;
//import com.smartclouder.framework.cloudbox.daemon.DaemonService;
import com.hantang.web.config.JsonConfig;
import com.hantang.web.json.handlers.HttpHandlerConstants;
 

public class MainService {
	
	private static Logger logger = Logger.getLogger(MainService.class);
	
	public static void main(String[] args) throws Exception{
		//final HttpServer server = HttpServer.createSimpleServer("/", JsonConfig.getInstance().getPort());
		HttpServer server = new HttpServer();
		NetworkListener networkListener = new NetworkListener("sample-listener", "0.0.0.0", JsonConfig.getInstance().getPort());
		
		ThreadPoolConfig threadPoolConfig = ThreadPoolConfig
		        .defaultConfig()
		        .setCorePoolSize(JsonConfig.getInstance().getCorePoolSize())
		        .setMaxPoolSize(JsonConfig.getInstance().getMaxiumPoolSize());
		networkListener.getTransport().setWorkerThreadPoolConfig(threadPoolConfig);
		server.addListener(networkListener);
		addHttpHandlers(server);  
		
		JsonConfig.getInstance();
//		StorageUtility.setCloudClient(JsonConfig.getInstance().getCloudBoxHost(), JsonConfig.getInstance().getCloudBoxPort());
		
	   // /*
	   // */
//		DaemonService.getInstance().executeJob(new DfsDatabaseInitDaemon());
		
		start(server);
	}

	public static void start(final HttpServer server) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					server.start();
				} catch (IOException ex) {
					logger.error(ex);
				}
				try {
					synchronized(this){
						this.wait();
					}
				} catch (InterruptedException ex) {
					logger.error(ex);
				}
			}
		}).start();
	}

	public  static void addHttpHandlers(final HttpServer server)throws IllegalAccessException, NoSuchFieldException,InstantiationException 
	{
		Class<HttpHandlerConstants> constantClass = HttpHandlerConstants.class;
		Field[] fields = constantClass.getFields();		
		JsonConfig.getInstance();
		
		for(Field field:fields){
			String fieldName = field.getName();
			if(fieldName.startsWith("API_") && String.class.equals(field.getType())){
				Object url = field.get(constantClass);
				logger.info("URL is "+url);				
				String hanlderFieldName = "HANDLER_"+fieldName;
				Field handlerField = constantClass.getField(hanlderFieldName);
				if(handlerField != null){
					logger.info("handler is "+handlerField);
					Class handlerClazz = (Class)handlerField.get(constantClass);
					if(handlerClazz == null){
						continue ;
					}
					Object hanlderObj = handlerClazz.newInstance();
					if(hanlderObj instanceof HttpHandler){
						logger.info("Add "+hanlderObj +" to handle "+url);
						server.getServerConfiguration().addHttpHandler((HttpHandler)hanlderObj, url.toString());
					}
				}
			}
		}
	}
}
